﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UniUI.ConsoleUI
{
    class ConsoleButton : IConsoleFocusControl
    {
        private IConsoleControl parent;
        private bool isFocused = false;

        public event Action Triggered;

        public ConsoleButton(IConsoleControl parent)
        {
            this.parent = parent;

            this.BackgroundColor = ConsoleColor.DarkGray;
            this.ForegroundColor = ConsoleColor.Black;
            this.ActiveBackgroundColor = ConsoleColor.White;
            this.ActiveForegroundColor = ConsoleColor.Black;
        }

        public int CursorX
        {
            get { return 0; }
        }

        public int CursorY
        {
            get { return 0; }
        }

        public bool ShowCursor
        {
            get { return false; }
        }

        public void GetFocus()
        {
            this.Invalidate();
            isFocused = true;
        }

        public void LostFocus()
        {
            this.Invalidate();
            isFocused = false;
        }

        public bool CanGetFocus
        {
            get { return true; }
        }

        public void OnKeyPress(ConsoleKeyInfo key, bool isInsertMode)
        {
            if (key.Key == ConsoleKey.Enter)
            {
                if (Triggered != null)
                {
                    Triggered.Invoke();
                }
            }
        }

        public IConsoleControl ParentControl
        {
            get { return parent; }
        }

        private ConsoleColor _ForegroundColor;
        public ConsoleColor ForegroundColor
        {
            get { return _ForegroundColor; }
            set
            {
                this.Invalidate();
                _ForegroundColor = value;
            }
        }

        private ConsoleColor _BackgroundColor;
        public ConsoleColor BackgroundColor
        {
            get { return _BackgroundColor; }
            set
            {
                this.Invalidate();
                _BackgroundColor = value;
            }
        }

        private ConsoleColor _ActiveForegroundColor;
        public ConsoleColor ActiveForegroundColor
        {
            get { return _ForegroundColor; }
            set
            {
                this.Invalidate();
                _ActiveForegroundColor = value;
            }
        }

        private ConsoleColor _ActiveBackgroundColor;
        public ConsoleColor ActiveBackgroundColor
        {
            get { return _ActiveBackgroundColor; }
            set
            {
                this.Invalidate();
                _ActiveBackgroundColor = value;
            }
        }

        private int _Left;
        public int Left
        {
            get { return _Left; }
            set
            {
                this.InvalidateParent();
                _Left = value;
            }
        }

        private int _Top;
        public int Top
        {
            get { return _Top; }
            set
            {
                this.InvalidateParent();
                _Top = value;
            }
        }

        private int _Width;
        public int Width
        {
            get { return _Width; }
            set
            {
                this.Invalidate();
                if (value > _Width)
                {
                    this.InvalidateParent();
                }
                _Width = value;
            }
        }

        private int _Height = 1;
        public int Height
        {
            get { return _Height; }
            set
            {
                this.Invalidate();
                if (value > _Height)
                {
                    this.InvalidateParent();
                }
                _Height = value;
            }
        }

        private string _Text;
        private int _TextLength;
        public string Text
        {
            get { return _Text; }
            set
            {
                this.Invalidate();
                _Text = ConsoleScreen.TruncateString(value, Width - 2);
                _TextLength = ConsoleScreen.GetStringLength(_Text);
            }
        }

        public void Render(ConsoleScreen screen)
        {
            //Always update cursor (it's not updated to Console here).
            if (isFocused)
            {
                screen.ShowFocus = false;
            }

            //Only draw when dirty.
            if (!dirty)
            {
                return;
            }

            dirty = false;

            int absX = this.GetAbsoluteLeft(), absY = this.GetAbsoluteTop();

            if (isFocused)
            {
                screen.ForegroundColor = ActiveForegroundColor;
                screen.BackgroundColor = ActiveBackgroundColor;
            }
            else
            {
                screen.ForegroundColor = ForegroundColor;
                screen.BackgroundColor = BackgroundColor;
            }

            screen.DrawRect(absX, absY, Width, Height);
            screen.DrawString(absX + (Width - _TextLength) / 2, absY + Height / 2, Text);
        }

        private bool dirty = true;

        public void Invalidate(int left, int top, int width, int height)
        {
            dirty = true;
        }
    }
}
